package com.hadwinling.alogriithm.apilearn.transfrom;

import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

/**
 * @description:
 * @author: hadwinling
 * @time: 2021/2/19 下午2:26
 */
public class TransfromTest1_Base {
    public static void main(String[] args) throws Exception {
        //
        StreamExecutionEnvironment executionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment();
        executionEnvironment.setParallelism(1);

        //从文件读取数据
        DataStream<String> DataStream = executionEnvironment.readTextFile("/home/hadoop/IdeaProjects/SparkScalaLearn/FlinkLearn/src/main/resources/datas/sensor.txt");

        //1 . map 把String 转换成长度输出
        DataStream<Integer> mapStream = DataStream.map(new MapFunction<String, Integer>() {
            @Override
            public Integer map(String s) throws Exception {
                return s.length();
            }
        });


        //2. flatmap ,按照逗号分字段
        DataStream<String> flatMapStream = DataStream.flatMap(new FlatMapFunction<String, String>() {
            @Override
            public void flatMap(String s, Collector<String> collector) throws Exception {
                String[] split = s.split(",");
                for (String field : split) {
                    collector.collect(field);
                }
            }
        });

        // 3 . filter  . 筛选sensor_1 开头的id对应的数据
        DataStream<String> filterStream = DataStream.filter(new FilterFunction<String>() {
            @Override
            public boolean filter(String s) throws Exception {
                return s.startsWith("sensor_1");
            }
        });

        // 打印输出
        mapStream.print("map");
        flatMapStream.print("flatMap");
        filterStream.print("filter");

        executionEnvironment.execute();


    }
}
